package com.workcell.workdata.im.component.validator;

import com.workcell.workdata.im.annotation.validator.MessageImageFormat;
import com.workcell.workdata.im.constant.MessageFormat;
import com.workcell.workdata.im.model.message.CloudImage;
import com.workcell.workdata.im.model.request.MessageRequest;
import com.workcell.workdata.im.util.JSON;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class MessageImageValidator implements ConstraintValidator<MessageImageFormat, MessageRequest> {

    @Override
    public boolean isValid(MessageRequest request, ConstraintValidatorContext context) {
        if (request.getFormat() != MessageFormat.IMAGE){
            return true;
        }

        CloudImage image = JSON.parseNullable(request.getContent(),CloudImage.class);

        return image != null
                && image.getThumb() != null
                && image.getImage() != null
                && image.getBucket() != null
                && image.getTh() != 0
                && image.getOh() != 0
                && image.getTw() != 0
                && image.getOw() != 0;
    }

}
